package com.hqyj.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hqyj.service.ShopService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 项目:rabbitmq-230301-provider
 * 描述:
 * 时间:2023/8/3 11:04
 * 作者:admin
 * 版本:1.0
 **/
@RestController
@RequestMapping("/test")
public class TestController {
    //mq发送消息的对象
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String send(){
        Map<String,Object> map = new HashMap<>();
        map.put("msg","你好");

        CorrelationData data = new CorrelationData(map.toString());
        //发送消息给mq(异步发送)
      // rabbitTemplate.convertAndSend("topicExchange","topic.ddd",map,data);
        rabbitTemplate.convertAndSend("orderExchange","orderRout",map,message -> {
            //设置消息过期 10 秒过期，单位是毫秒
            message.getMessageProperties().setExpiration("10000");
            return message;
        });
        //发送消息给mq(同步发送)
        //Object obj = rabbitTemplate.convertSendAndReceive("directExchange","directRout01",map);
        return "success";
    }
    @GetMapping("/send01")
    public String send01(){
        Map<String,Object> map = new HashMap<>();
        map.put("msg","world");
        //发送消息给mq(异步发送)
        rabbitTemplate.convertAndSend("topicExchang","www.ddd",map);
        //发送消息给mq(同步发送)
        //Object obj = rabbitTemplate.convertSendAndReceive("directExchange","directRout01",map);
        return "success";
    }

    @Autowired
    ShopService shopService;
    @GetMapping("/kill")
    public HashMap<String,Object> kill(String shopName,String userName) throws JsonProcessingException, InterruptedException {
        return shopService.buy(shopName,userName);
    }

}
